package test;

import io.rsocket.ConnectionSetupPayload;
import io.rsocket.Payload;
import io.rsocket.RSocket;
import io.rsocket.SocketAcceptor;
import io.rsocket.core.RSocketConnector;
import io.rsocket.transport.netty.client.TcpClientTransport;
import io.rsocket.util.DefaultPayload;
import io.rsocket.util.RSocketProxy;
import reactor.core.publisher.Mono;

public class Client001 {

    public static void main(String[] args) throws Exception {

        TcpClientTransport transport = TcpClientTransport.create(80);

        Payload setupPayload = DefaultPayload.create("ab","b=1");
        Mono<RSocket> rSocketMono = RSocketConnector
                .create()
                .setupPayload(setupPayload)
                .acceptor(new SocketAcceptor() {
                    @Override
                    public Mono<RSocket> accept(ConnectionSetupPayload setup, RSocket sendingSocket) {
                        return Mono.just(new RSocketProxy(sendingSocket));
                    }
                })
                .connect(transport);

        RSocket rSocket = rSocketMono
                .doOnError(e-> {
                    e.printStackTrace();
                }).block();

        rSocket.requestResponse(DefaultPayload.create("Hello"))
                .map(Payload::getDataUtf8)
                .doOnNext(s -> System.out.println(s))
                .doOnError(e-> {
                    e.printStackTrace();
                })
                .block();

        Sleeps.waitForExit();

    }
}
